package com.yuneec.fly.utils.inject;

import android.content.Context;
import android.util.Xml;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by gjw on 2016/12/22.
 */

public class WifiPasswordsSaver {

    public static List<WifiPassword> getWifiPasswords(Context context) {
        InputStream mInputStream = null;
        try {
            File xmlFile = new File(context.getDir("assets", 0).toString() + "/wifi_ssid_password.xml");
            mInputStream = new FileInputStream(xmlFile);
            List<WifiPassword> wifiPasswords = null;
            WifiPassword wifiPassword = null;
            XmlPullParser parser = Xml.newPullParser(); //由android.util.Xml创建一个XmlPullParser实例
            parser.setInput(mInputStream, "UTF-8");               //设置输入流 并指明编码方式
            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        wifiPasswords = new ArrayList<WifiPassword>();
                        break;
                    case XmlPullParser.START_TAG:
                        if (parser.getName().equals("wifipassword")) {
                            wifiPassword = new WifiPassword();
                        } else if (parser.getName().equals("ssid")) {
                            eventType = parser.next();
                            wifiPassword.ssid = parser.getText();
                        } else if (parser.getName().equals("password")) {
                            eventType = parser.next();
                            wifiPassword.password = parser.getText();
                        } else if (parser.getName().equals("cipherType")) {
                            eventType = parser.next();
                            wifiPassword.cipherType = Integer.parseInt(parser.getText());
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if (parser.getName().equals("wifipassword")) {
                            wifiPasswords.add(wifiPassword);
                            wifiPassword = null;
                        }
                        break;
                }
                eventType = parser.next();
            }
            return wifiPasswords;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                mInputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return new ArrayList<>();
    }

    public static String getWifiPassword(Context context, String ssid) {
        List<WifiPassword> wifiPasswords = getWifiPasswords(context);
        for (WifiPassword wifiPassword : wifiPasswords) {
            if (ssid.equals(wifiPassword.ssid)) {
                return wifiPassword.password;
            }
        }
        return "";
    }

    public static void saveWifiPasswords(Context context, List<WifiPassword> wifiPasswords) {
        FileOutputStream outStream = null;
        try {
            File xmlFile = new File(context.getDir("assets", 0).toString() + "/wifi_ssid_password.xml");
            outStream = new FileOutputStream(xmlFile);
            XmlSerializer serializer = Xml.newSerializer();
            serializer.setOutput(outStream, "UTF-8");
            serializer.startDocument("UTF-8", true);
            serializer.startTag(null, "wifipasswords");
            for (WifiPassword wifiPassword : wifiPasswords) {
                serializer.startTag(null, "wifipassword");
                serializer.startTag(null, "ssid");
                serializer.text(wifiPassword.ssid);
                serializer.endTag(null, "ssid");
                serializer.startTag(null, "password");
                serializer.text(wifiPassword.password);
                serializer.endTag(null, "password");
                serializer.startTag(null, "cipherType");
                serializer.text("" + wifiPassword.cipherType);
                serializer.endTag(null, "cipherType");
                serializer.endTag(null, "wifipassword");
            }
            serializer.endTag(null, "wifipasswords");
            serializer.endDocument();
            outStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                outStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static class WifiPassword {
        public String ssid;
        public String password;
        public int cipherType = -1;

        @Override
        public String toString() {
            return "WifiPassword [ssid=" + ssid + ", password=" + password + ", cipherType=" + cipherType + "]";
        }

        public WifiPassword(String ssid, String password, int cipherType) {
            super();
            this.ssid = ssid;
            this.password = password;
            this.cipherType = cipherType;
        }

        public WifiPassword(String ssid, String password) {
            this.ssid = ssid;
            this.password = password;
        }

        public WifiPassword() {
        }
    }
}
